package com.test.web.jdbc;

import com.test.web.jdbc.execute.ExecuteQuery;
import com.test.web.jdbc.execute.ExecuteUpdate;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * com.test.web.component
 *
 * @author 白雨浓
 * date: 2018/10/21 11:14
 * content:
 */
@Component
public class JdbcTemplate {

    public JdbcConnection init(String database, String username, String password) {
        return new JdbcConnection(database, username, password);
    }

    public JdbcConnection init(String host, Integer port, String database, String username, String password) {
        return new JdbcConnection(host, port, database, username, password);
    }

    public <T> T selectOne(String sql, JdbcConnection jdbcConnection, Class<T> clz, Map<String, Object> paramMap) {
        return new ExecuteQuery(sql, jdbcConnection).exec(clz, paramMap);
    }

    public <T> T selectOne(String sql, JdbcConnection jdbcConnection, Class<T> clz) {
        return new ExecuteQuery(sql, jdbcConnection).exec(clz, new HashMap<>());
    }

    public long selectCount(String sql, JdbcConnection jdbcConnection, Map<String, Object> paramMap) {
        return new ExecuteQuery(sql, jdbcConnection).execCount(paramMap);
    }

    public long selectCount(String sql, JdbcConnection jdbcConnection) {
        return new ExecuteQuery(sql, jdbcConnection).execCount(new HashMap<>());
    }

    public <T> List<T> selectList(String sql, JdbcConnection jdbcConnection, Class<T> clz, Map<String, Object> paramMap) {
        return new ExecuteQuery(sql, jdbcConnection).execList(clz, paramMap);
    }

    public <T> List<T> selectList(String sql, JdbcConnection jdbcConnection, Class<T> clz) {
        return new ExecuteQuery(sql, jdbcConnection).execList(clz, new HashMap<>());
    }

    public <T> int insert(String sql, JdbcConnection jdbcConnection, T target) {
        return new ExecuteUpdate(sql, jdbcConnection).exec(target, new HashMap<>());
    }

    public <T> int insert(String sql, JdbcConnection jdbcConnection, T target, Map<String, Object> paramMap) {
        return new ExecuteUpdate(sql, jdbcConnection).exec(target, paramMap);
    }

    public <T> int update(String sql, JdbcConnection jdbcConnection, T target) {
        return new ExecuteUpdate(sql, jdbcConnection).exec(target, new HashMap<>());
    }

    public <T> int update(String sql, JdbcConnection jdbcConnection, T target, Map<String, Object> paramMap) {
        return new ExecuteUpdate(sql, jdbcConnection).exec(target, paramMap);
    }

    public int delete(String sql, JdbcConnection jdbcConnection) {
        return new ExecuteUpdate(sql, jdbcConnection).exec(null, new HashMap<>());
    }

    public int delete(String sql, JdbcConnection jdbcConnection, Map<String, Object> paramMap) {
        return new ExecuteUpdate(sql, jdbcConnection).exec(null, paramMap);
    }
}
